package com.azt.front.action.content;

import com.azt.api.pojo.Advertise;
import com.azt.api.pojo.Channel;
import com.azt.api.pojo.Content;
import com.azt.api.service.AdvertiseService;
import com.azt.api.service.ChannelService;
import com.azt.api.service.ContentService;
import com.azt.front.action.BaseAction;
import com.azt.model.page.Pagination;
import com.azt.utils.BCrypt;
import com.xiaoleilu.hutool.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.WebUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ContentAction
 * @Description  内容新闻管理action
 * @author Snowler,zdd
 * @Date 2016年8月2日 上午10:50:05
 * @version 1.0.0
 */
@Controller
@RequestMapping("/content")
public class ContentAction extends BaseAction {
	
	@Autowired
	private ContentService cs;
	
	@Autowired
	private ChannelService chs;
	
	@Autowired
	private AdvertiseService advertiseService;
	
	/**
	 * 右侧菜单
	 * @Title: getReadsAndBusiness 
	 * @Description: TODO
	 * @param model
	 * @return: void
	 * @author: zhoudongdong 2017年2月9日 上午10:55:38
	 */
	public void getReadsAndBusiness(Model model){
		Channel a=chs.getChannelByCode("HOT_BUSINESS");
		List<Content> hotBusiness=cs.selectContentByChannelCode("HOT_BUSINESS", 4);
		//热门阅读，取阅读量最高的5条
		List<Content> hotReads=cs.findHotRead();
		model.addAttribute("bus", a);
		model.addAttribute("hotbus", hotBusiness);
		model.addAttribute("hotRed", hotReads);
	}
	
	/**
	 * 新闻列表页
	 */
	@RequestMapping("/list/{channelId}")
	public String list(@PathVariable Integer channelId, Integer pageNo, Model model) {
		Map<String , String> searchMap = new HashMap<String ,String>();
		if(channelId != null){
			searchMap.put("so_channelId", channelId.toString());
			model.addAttribute("channel", chs.getChannelById(channelId));//所属栏目
		}
		Pagination contentPage = cs.getContentPage(pageNo == null ? 1 : pageNo,5, searchMap);//列表分页数据
		getReadsAndBusiness(model);
		model.addAttribute("contentPage" , contentPage);
		model.addAttribute("so_channelId", channelId.toString());
		return "info/infoList";
	}
	
	/**
	 * ajax分页
	 * @Title: contentListPage 
	 * @Description: TODO
	 * @param mv
	 * @param channelId
	 * @param pageIndex
	 * @return
	 * @return: ModelAndView
	 * @author: zhoudongdong 2017年2月9日 下午2:32:48
	 */
	@RequestMapping("/contentListPage")
	@ResponseBody
	public ModelAndView contentListPage(ModelAndView mv,String channelId,Integer pageIndex){
		Map<String , String> searchMap = new HashMap<String ,String>();
		searchMap.put("so_channelId", channelId);
		Pagination contentPage = cs.getContentPage(pageIndex == null ? 1 : pageIndex,5, searchMap);//列表分页数据
		mv.addObject("contentPage", contentPage);
		mv.setViewName("info/infoListPage");
		return mv;
	}
	
	/**
	 * 新闻详情页
	 */
	@RequestMapping("/detail/{id}")
	public String detail(@PathVariable Integer id, Model model) {
		Advertise adv= advertiseService.getAdvertiseByCode("INFO_FOOT_ADV");
		model.addAttribute("adv", adv);
		if(id != null){
			model.addAttribute("content" , cs.getContentById(id));
			cs.updateHitAdd(id);
		}
		getReadsAndBusiness(model);
		return "info/infoArticle";
	}

	@RequestMapping("/preview")
	public String preview(Model model) {
		String token = getPara("token");
		if(StrUtil.isNotBlank(token)) {
			if(BCrypt.checkpw("azt_news_preview", token)) {
				Integer channelId = getParaToInt("channelId");
				if(channelId != null) {
					Channel channel = chs.getChannelById(channelId);
					if(channel != null) {
						getReadsAndBusiness(model);
						model.addAttribute("channel", channel);
						model.addAllAttributes(WebUtils.getParametersStartingWith(request, null));
						return "info/preview";
					}
				}
			}
		}
		return "redirect:/";
	}

	@RequestMapping("/infoIndex")
	public String contentIndex(Model model){
		String[] menuarr=new String[]{"INDUSTRY_NEWS","HI_TECH","BIG_EVENT"};
		List<Content> recommendInfo=cs.selectListHotChannel(menuarr);
		List<Channel> channelMenu=chs.getChannelListByCodesArr(menuarr);
		model.addAttribute("recommendInfo", recommendInfo);//三个菜单的content（轮播下面的三个）
		model.addAttribute("menu", channelMenu);//三个菜单
		
		//默认显示数组第一个菜单
		//Map<String , String> searchMap = new HashMap<String ,String>();
		Channel defaultChannel=chs.getChannelByCode(menuarr[0]);
		//searchMap.put("so_channelId", defaultChannel.getId().toString());
		//Pagination contentPage = cs.getContentPage(1,3, searchMap);//列表分页数据
		getReadsAndBusiness(model);
		//model.addAttribute("contentPage" , contentPage);
		model.addAttribute("so_channelId",  defaultChannel.getId().toString());
		
		return "info/infoIndex";
		
	}
	
	
	@RequestMapping("/contentIndexPage")
	@ResponseBody
	public ModelAndView contentIndexPage(ModelAndView mv,String channelId,Integer pageIndex){
		Map<String , String> searchMap = new HashMap<String ,String>();
		searchMap.put("so_channelId", channelId);
		Pagination contentPage = cs.getContentPage(pageIndex == null ? 1 : pageIndex,3, searchMap);//列表分页数据
		mv.addObject("contentPage", contentPage);
		mv.addObject("so_channelId", channelId);
		mv.setViewName("info/infoIndexPage");
		return mv;
	}
}
